package edu.tyut.game.ui.community

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import edu.tyut.game.logic.FriendCircleRepository
import edu.tyut.game.logic.model.Comment
import edu.tyut.game.utils.LogUtils
import okhttp3.MultipartBody

class CommunityViewModel : ViewModel() {
    private val tag = this::class.java.name
    // 编辑朋友圈
    // private val editFriendCircleLiveData = MutableLiveData<List<MultipartBody.Part>>()
    // val resEditFriendCircle = editFriendCircleLiveData.switchMap {
    //     FriendCircleRepository.updateFriendCircle(it)
    // }
    // fun editFriendCircle(parts: List<MultipartBody.Part>){
    //     editFriendCircleLiveData.value = parts
    // }
    // 删除朋友圈
    private val deleteFriendCircleLiveData = MutableLiveData<Int>()
    val  resDeleteFriendCircle = deleteFriendCircleLiveData.switchMap {
        FriendCircleRepository.deleteFriendCircle(it)
    }
    fun deleteFriendCircle(fcId: Int){
        deleteFriendCircleLiveData.value = fcId
    }
    // 查询朋友圈
    private val queryFriendCircleLiveData = MutableLiveData<String>()
    val resFriendCircles = queryFriendCircleLiveData.switchMap {
        FriendCircleRepository.queryFriendCircle(it)
    }
    fun queryFriendCircle(order: String){
        queryFriendCircleLiveData.value = order
    }
    // 朋友圈点赞 + 1
    private val addFriendCircleLikeLiveData = MutableLiveData<Int>()
    val resAddFriendCircleLike = addFriendCircleLikeLiveData.switchMap {
        FriendCircleRepository.addFriendCircleLike(it)
    }
    fun addFriendCircleLike(fcId: Int){
        addFriendCircleLikeLiveData.value = fcId
    }
    // 朋友圈点赞 + 1
    private val subFriendCircleLikeLiveData = MutableLiveData<Int>()
    val resSubFriendCircleLike = subFriendCircleLikeLiveData.switchMap {
        FriendCircleRepository.subFriendCircleLike(it)
    }
    fun subFriendCircleLike(fcId: Int){
        subFriendCircleLikeLiveData.value = fcId
    }

    // 发送评论
    private val commentLiveData = MutableLiveData<Comment>()
    val resComment = commentLiveData.switchMap {
        FriendCircleRepository.postComment(it)
    }
    fun postComment(comment: Comment){
        commentLiveData.value = comment
    }
    // 查询评论
    private val commentsLiveData = MutableLiveData<Int>()
    val resComments = commentsLiveData.switchMap {
        LogUtils.d(tag, "查询评论: $it")
        FriendCircleRepository.queryComment(it)
    }
    fun queryComment(fcId: Int){
        commentsLiveData.value = fcId
    }
    // 删除评论
    private val deleteCommentLiveData = MutableLiveData<Int>()
    val resDeleteComment = deleteCommentLiveData.switchMap {
        FriendCircleRepository.deleteComment(it)
    }
    fun deleteComment(id: Int){
        deleteCommentLiveData.value = id
    }
    // 评论点赞 + 1
    private val addCommentLikeLiveData = MutableLiveData<Int>()
    val resAddCommentLike = addCommentLikeLiveData.switchMap {
        FriendCircleRepository.addCommentLike(it)
    }
    fun addCommentLike(commentId: Int){
        addCommentLikeLiveData.value = commentId
    }
    // 评论取消点赞 - 1
    private val subCommentLikeLiveData = MutableLiveData<Int>()
    val resSubCommentLike = subCommentLikeLiveData.switchMap {
        FriendCircleRepository.subCommentLike(it)
    }
    fun subCommentLike(commentId: Int){
        subCommentLikeLiveData.value = commentId
    }
}